package com.iteima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iteima.reggie.common.R;
import com.iteima.reggie.dto.SetmealDto;
import com.iteima.reggie.entity.Category;
import com.iteima.reggie.entity.Dish;
import com.iteima.reggie.entity.Setmeal;
import com.iteima.reggie.entity.SetmealDish;
import com.iteima.reggie.service.CategoryService;
import com.iteima.reggie.service.SetmealDishService;
import com.iteima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    /**
     * 分页查询
     * @param page
     * @param name
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page( int page, String name, int pageSize){
        log.info("page:{},name:{},pageSize:{}");
        //构造分页构造器
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);

        Page<SetmealDto> dtoPage = new Page<>();

        //构造分页条件
        LambdaQueryWrapper<Setmeal>queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(null != name,Setmeal::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo,queryWrapper);

        //构造dtoPage分页; pageInfo里面的 List<Setmeal> records;
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> setmealDtoRecords = records.stream().map(item ->{
            SetmealDto setmealDto = new SetmealDto();
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            setmealDto.setCategoryName(categoryName);

            BeanUtils.copyProperties(item,setmealDto);
            return setmealDto;

        }).collect(Collectors.toList());

        //赋值
        dtoPage.setRecords(setmealDtoRecords);
        return R.success(dtoPage);
    }


    /**
     * 保存套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("setmealDto：{}");
        setmealService.saveWithDish(setmealDto);

        return R.success("新增套餐成功!");
    }

    /**
     * 通过套餐id返回SetmealDto
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getSetmealDtoById(@PathVariable("id") Long id){
        log.info("id:{}",id);
        //
        Setmeal setmeal = setmealService.getById(id);
        //获取CategoryName
        Long categoryId = setmeal.getCategoryId();
        Category category = categoryService.getById(categoryId);
        String categoryName = category.getName();
 
        //构造对象
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        //封装分类名称
        setmealDto.setCategoryName(categoryName);
        LambdaQueryWrapper<SetmealDish> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setmealDishService.list(queryWrapper);
        //封装套餐菜品
        setmealDto.setSetmealDishes(setmealDishes);


        return R.success(setmealDto);
    }
    @DeleteMapping
    public R<String> deleteById(@RequestParam List<Long> ids){
        log.info("ids={}",ids);
        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功!");
    }

    /**
     * 通过套餐id获取套餐信息
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public  R<List<Setmeal>> list(Setmeal setmeal){//注意在前端传过来的值不是json格式而是key-value的形式,所以不用加@RequestBody
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        List<Setmeal> list = setmealService.list(queryWrapper);
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);


        setmealService.list(queryWrapper);
        return R.success(list);
    }

    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        log.info(setmealDto.toString());
        setmealService.saveWithSetmealDish(setmealDto);
        return R.success("修改成功");
    }

}
